<!-- subpkg_consult/patient/index.vue -->
<script setup>
	import { ref, computed } from 'vue'
	import { onShow } from '@dcloudio/uni-app'
	import { patientListApi, removePatientApi } from '@/services/patinet.js'
	import useConsultStore from '@/stores/consult.js'

	const consultStore = useConsultStore()
	// 侧滑按钮配置
	const swipeOptions = ref([
		{
			text: '删除',
			style: {
				backgroundColor: '#dd524d',
			},
		},
	])
	//患者列表
	const patinetList = ref([])
	const getPatinetList = async () => {
		const res = await patientListApi()
		//渲染列表数据
		patinetList.value = res.data
		// 设置默认值
		if (!consultStore.patientId) {
			const newStr = res.data.filter((v) => v.defaultFlag === 1)[0].id
			consultStore.setPatientId(newStr)
		} else {
			console.log('存在')
		}

		// 展示页面

		//   pageshow.value = true
	}
	// 删除患者信息
	const onSwipeActionClick = async (id, i) => {
		const res = await removePatientApi(id)
		uni.utils.toast('删除成功')
		// Vue 实例中的数据也要同步删除
		patinetList.value.splice(i, 1)
	}

	// 患者卡片索引值
	const patientCardIndex = ref(0)
	// 选择患者
	function onPatientCardClick(index, id) {
		// 患者的索引值
		patientCardIndex.value = index
		consultStore.setPatientId(id)
	}
	// 下一步跳转
	function nextBtn() {
		uni.navigateTo({
			url: '/subpkg_consult/payment/index',
		})
	}
	// 返回页面刷新数据
	onShow(() => {
		getPatinetList()
	})
</script>
<template>
	<scroll-page>
		<view class="patient-page">
			<view class="page-header">
				<view class="patient-title"> 请选择患者信息 </view>
				<view class="patient-tips">
					以便医生给出更准确的治疗，信息仅医生可见
				</view>
			</view>
			<uni-swipe-action>
				<uni-swipe-action-item
					:right-options="swipeOptions"
					v-for="(v, i) in patinetList"
					:key="v.id + '_' + v.defaultFlag"
					@click="onSwipeActionClick(v.id, i)"
				>
					<view
						@click="onPatientCardClick(i, v.id)"
						class="archive-card"
						:class="{ active: patientCardIndex === i }"
					>
						<view class="archive-info">
							<text class="name">{{ v.name }}</text>
							<text class="id-card">
								{{
									v.idCard.replace(
										/^(.{6}).+(.{4})$/,
										'$1********$2'
									)
								}}</text
							>
							<text class="default" v-if="v.defaultFlag === 1"
								>默认</text
							>
						</view>
						<view class="archive-info">
							<text class="gender">{{ v.genderValue }}</text>
							<text class="age">{{ v.age }}岁</text>
						</view>
						<navigator
							hover-class="none"
							class="edit-link"
							:url="`/subpkg_archive/form/index?id=${v.id}`"
						>
							<uni-icons
								type="icon-edit"
								size="20"
								color="#16C2A3"
								custom-prefix="iconfont"
							/>
						</navigator>
					</view>
				</uni-swipe-action-item>
			</uni-swipe-action>

			<!-- 添加按钮 -->
			<view v-if="true" class="archive-card">
				<navigator
					class="add-link"
					hover-class="none"
					url="/subpkg_archive/form/index"
				>
					<uni-icons color="#16C2A3" size="24" type="plusempty" />
					<text class="label">添加患者</text>
				</navigator>
			</view>
		</view>
		<!-- 下一步操作 -->
		<view class="next-step">
			<button class="uni-button" @click="nextBtn">下一步</button>
		</view>
	</scroll-page>
</template>

<style lang="scss">
	@import './index.scss';
</style>
